import { notification as antdNOtification } from 'antd';
import useNotificationHooks from 'antd/lib/notification/useNotification';
import React from 'react';
import { CloseIcon, ErrorIcon, InfoIcon, SuccessIcon, WarnIcon } from '../Svg';

// type
import type { ArgsProps } from 'antd/lib/notification';
import type {
  GlobalConfigProps,
  IconType,
  NotificationInstance,
} from 'antd/lib/notification/interface';
export type {
  IconType,
  NotificationPlacement,
} from 'antd/lib/notification/interface';

interface BaseMethods {
  open: (config: ArgsProps) => void;
  destroy: (key?: React.Key) => void;
  config: (config: GlobalConfigProps) => void;
  useNotification: typeof useNotificationHooks;
  /** @private Internal Component. Do not use in your production. */
  // _InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
}
type StaticFn = (config: ArgsProps) => void;
interface NoticeMethods {
  success: StaticFn;
  info: StaticFn;
  warning: StaticFn;
  error: StaticFn;
}
export type NotificationApi = NoticeMethods & BaseMethods;

export type Notification = NotificationApi;
export type NotificationApiKey = IconType | 'open';
export type NotificationTypeApi = NotificationInstance & {
  warn(args: ArgsProps): void;
};

const icons: {
  [key in NotificationApiKey]: (() => JSX.Element) | null;
} = {
  success: SuccessIcon,
  info: InfoIcon,
  error: ErrorIcon,
  warning: WarnIcon,
  open: null,
};
const Keys: NotificationApiKey[] = [
  'success',
  'info',
  'error',
  'warning',
  'open',
];

// 二次处理api
const api: NotificationTypeApi = Keys.reduce((api, key) => {
  api[key] = (args: ArgsProps) => {
    const icon = icons[key];
    const params = { icon: icon && icon(), closeIcon: <CloseIcon />, ...args };
    antdNOtification[key].call(null, params);
  };
  return api;
}, {} as NotificationTypeApi);

const { config, destroy, useNotification } = antdNOtification;
const notification: NotificationApi = {
  ...api,
  config,
  destroy,
  useNotification,
};

export default notification;
